package org.mxp.utils;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Component
public class TableMessageUtil {

    /**
     * 表格信息
     */
    private Map<String, String> tableData = new HashMap<>();

    public void addCell(String cellStr) {
        if (cellStr != null) {
            String[] cellData = cellStr.split("#");
            if (cellData.length == 2) {
                tableData.put(cellData[0], cellData[1]);
            }
        }
    }

    public String getAllCell() {
        String result = "";
        Iterator<Map.Entry<String, String>> iterator = tableData.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            result += entry.getKey() + "#" + entry.getValue() + "|";
        }
        if (!result.isEmpty()) {
            result = result.substring(0, result.length() - 1);
        }
        return result;
    }

    public void initAllCell(String cellStr) {
        this.clear();
        if (StringUtils.isEmpty(cellStr)) {
            return;
        }
        String[] allCell = cellStr.split("\\|");
        for (String cell: allCell) {
            this.addCell(cell);
        }
    }

    public void addListCell(String cellStr) {
        if (StringUtils.isEmpty(cellStr)) {
            return;
        }
        String[] allCell = cellStr.split("\\|");
        for (String cell: allCell) {
            this.addCell(cell);
        }
    }

    public void clear() {
        tableData.clear();
    }

}
